package sctu.edu.java.year2016.team01.renxinpeng.day20160522.chapter4_3;

/**
 * 编写Java程序，使用while循环语句计算1+1/2！+1/3!...1/20!之和。
 */
public class Chapter4_3 {
    static int factorial(int x) {
        int sum = 1;
        for (int i = 1; i <= x; i++) {
            sum *= i;
        }
        return sum;

    }

    public static void main(String[] args) {
        int i = 1;
        double sum = 0;
        while (i <= 20) {
            sum += 1 / (double) factorial(i);
            i++;
        }
        System.out.println("sum=" + sum);
    }
}
